/*
 *
 */

jsx3.Class.defineClass(
        "tibco.admin.enterpriseAssets.shdResDefs.JNDI.ShdJndiResDefs", //the full name of the class to create
        jsx3.lang.Object, //  extends class
        [tibco.admin.enterpriseAssets.shdResDefs.ShdResDefsInterface], // implements interface
        function (plugin) {

            plugin.prototype.details = null;
            // TODO: handle multiple instances?

            // Implementation of the ShdResDefsInterface
            plugin.instance = null

            plugin.prototype.initPlugin = function() {
                tibco.admin.Main.loadResource("tibco/admin/enterpriseAssets/shdResDefs/JNDI/jss/jss.xml", "tibco.admin", "jss_25C", "ljss", false);
                tibco.admin.Main.loadResource("tibco/admin/enterpriseAssets/shdResDefs/JNDI/jss/valueJSS.xml", "tibco.admin", "value_25C", "jss", false);
            }

            plugin.prototype.destroyPlugin = function() {
            }

            plugin.getInstance = function(args) {
                if(!this.instance) {
                    this.instance = new tibco.admin.enterpriseAssets.shdResDefs.JNDI.ShdJndiResDefs();
                }
                return this.instance;
            }

            plugin.prototype.loadComponent = function(parent, master, id, name, description) {
                var me = this;
                this.details = new tibco.admin.enterpriseAssets.shdResDefs.JNDI.ShdJndiResDefDetails(master, parent, id,
                        name, description, me);
                this.details.loadView();
                return this.details;
            }

            plugin.prototype.getType = function() {
                return "JNDI";
            }

            plugin.prototype.getAddText = function() {
                return tibco.admin.Main.getServer().getDynamicProperty("@AddText_JNDI");
            }

            plugin.prototype.getImagePath = function() {
                return tibco.admin.Main.getServer().getDynamicProperty("@addin@JNDI Icon Path");
            }

            plugin.prototype.addSharedResourceDefinition = function(parent, master) {
                var me = this;
                this.details = new tibco.admin.enterpriseAssets.shdResDefs.JNDI.ShdJndiResDefDetails(master, parent, null,
                        null, null, me);
                this.details.loadAdd();
                return this.details;
            }
            // End implementation of the ShdResDefsInterface

        });

jsx3.Class.defineClass(
        "tibco.admin.enterpriseAssets.shdResDefs.JNDI.ShdJndiResDefDetails", //the full name of the class
        tibco.admin.enterpriseAssets.shdResDefs.ShdResDefDetails, //  extends ShdResDefDetails
        null, // implements no interface
        function (details) {
            details.ONADDPROPERTY = "tibco.admin.enterpriseAssets.shdResDefs.JNDI.ADDPROPERTY";
            details.ONADDOK = "tibco.admin.enterpriseAssets.shdResDefs.JNDI.ADDOK";
            details.ONCHANGEPROPERTY = "tibco.admin.enterpriseAssets.shdResDefs.JNDI.CHANGEPROPERTY";
            details.ONREMOVEPROPERTY = "tibco.admin.enterpriseAssets.shdResDefs.JNDI.REMOVEPROPERTY";
            details.ONSELECTPROPERTY = "tibco.admin.enterpriseAssets.shdResDefs.JNDI.SELECTPROPERTY";
            details.ONCTXFACTORYSELECTED = "tibco.admin.enterpriseAssets.shdResDefs.JNDI.ONCTXFACTORYSELECTED";

            details.prototype.dialog = null;
            details.prototype.helpID = "EnterpriseAssets.SharedResourceDefinitions.SharedResourceDefinitions.JNDI";

            details.prototype.init = function(master, parent, id, name, description, handler) {
                this.jsxsuper(master, parent, id, name, description, handler);
                this.editableComponentList.addAll(["cmbCtxtFactory_25D", "txtCtxtURL_25D", "txtUserName_25D",
                        "txtPassword_25D", "radValSecCtxtYes_25D", "radValSecCtxtNo_25D"], this.editableComponentList.size());
                this.rwComponentList.addAll(["cmbCtxtFactory_25D", "txtCtxtURL_25D", "txtUserName_25D", "txtPassword_25D",
                        "blkValSecCtxtYesNo_25D", "btnAppPropertiesRemove_25D", "btnAppPropertiesAdd_25D"], this.rwComponentList.size());
                this.roComponentList.addAll(["lblcCtxtFactory_25D", "lblcCtxtURL_25D", "lblcUserName_25D",
                        "lblcPassword_25D", "lblcValSecCtxt_25D"], this.roComponentList.size());
            }

            details.prototype.getType = function() {
                return tibco.admin.Main.getServer().getDynamicProperty("@SRDType_JNDI");
            }

            details.prototype.getHelpId = function() {
                return this.helpID;
            }

            details.prototype.getDetailsOperation = function() {
                return "getJndiResourceDetails";
            }
            details.prototype.getDetailsOperationRules = function() {
                return "tibco/admin/enterpriseAssets/shdResDefs/JNDI/rules/getJndiResourceDetails.xml";
            }
            details.prototype.editOperation = function() {
                return "editJndiResource";
            }
            details.prototype.editOperationRules = function() {
                return "tibco/admin/enterpriseAssets/shdResDefs/JNDI/rules/editJndiResource.xml";
            }
            details.prototype.addOperation = function() {
                return "addJndiResource";
            }
            details.prototype.addOperationRules = function() {
                return "tibco/admin/enterpriseAssets/shdResDefs/JNDI/rules/addJndiResource.xml";
            }
            details.prototype.getDetailsComponent = function() {
                return "tibco/admin/enterpriseAssets/shdResDefs/JNDI/components/shdJndiResDefDetails.xml";
            }
            details.prototype.getSecondaryDetailsComponent = function() {
                return "tibco/admin/enterpriseAssets/shdResDefs/JNDI/components/shdJndiResDefAttributes.xml";
            }

            // TODO: make name unique
            details.prototype.getDefaultName = function() {
                return tibco.admin.Main.getServer().getDynamicProperty("@defaultName_JNDI");
            }
            details.prototype.getDefaultDescription = function() {
                return tibco.admin.Main.getServer().getDynamicProperty("@defaultDescription_JNDI");
            }

            details.prototype.initializeSpecificDetails = function(objEvent) {
                if(objEvent.status == "SUCCESS") {
                    this.initCtxtFactory();

					objEvent.obj.handler = this;//070818
                }
                this.jsxsuper(objEvent);
            }

            details.prototype.initializeSpecificAdd = function(objEvent) {
                if(objEvent.status == "SUCCESS") {
                    this.initCtxtFactory();

					objEvent.obj.handler = this;//070818
                    this.initializeJNDIParam(objEvent);
                }
                this.jsxsuper(objEvent);
            }

            details.prototype.initCtxtFactory = function() {
                var str = '<data jsxid="jsxroot">';
                str += '<record jsxid="com.tibco.tibjms.naming.TibjmsInitialContextFactory" jsxtext="' + tibco.admin.Main.getServer().getDynamicProperty("@@com.tibco.tibjms.naming.TibjmsInitialContextFactory_25D") + '" />';
                str += '</data>';
                var sel = this.mainComponent.getDescendantOfName("cmbCtxtFactory_25D");
                tibco.admin.Main.getServer().getCache().clearById(sel.getXMLId());
                sel.setXMLString(str);
                sel.setValue(tibco.admin.Main.getServer().getDynamicProperty("@@com.tibco.tibjms.naming.TibjmsInitialContextFactory_25D"), true);
                sel.repaint();
                sel.subscribe(details.ONCTXFACTORYSELECTED, this, this.selectCtxFactory);
            }

            details.prototype.selectCtxFactory = function(event) {
                var txt = this.mainComponent.getDescendantOfName("txtCtxtURL_25D");
                switch(this.mainComponent.getDescendantOfName("cmbCtxtFactory_25D").getValue()) {
                    case tibco.admin.Main.getServer().getDynamicProperty("@@com.tibco.tibjms.naming.TibjmsInitialContextFactory_25D"):
                        txt.setValue(tibco.admin.Main.getServer().getDynamicProperty("@@tibjmsnaming_25D"), true);
                        break;
                    default:
                        break;
                }
            }
            details.prototype.initializeSecondaryDetails = function(objEvent) {
                if(objEvent.status == "SUCCESS") {
                    this.setEventCallbacks();
                }
                this.jsxsuper(objEvent);
            }

            details.prototype.initializeSecondaryAdd = function(objEvent) {
                if(objEvent.status == "SUCCESS") {
                    this.setEventCallbacks();
                }
                this.jsxsuper(objEvent);
            }

            details.prototype.setEventCallbacks = function() {
                this.mainComponent.getDescendantOfName("btnAppPropertiesAdd_25D").subscribe(details.ONADDPROPERTY,
                        this, this.onAddProperty);
                this.mainComponent.getDescendantOfName("btnAppPropertiesRemove_25D").subscribe(details.ONREMOVEPROPERTY,
                        this, this.onRemoveProperty);
                this.mainComponent.getDescendantOfName("mtxAppPropertiesList_25D").subscribe(details.ONSELECTPROPERTY,
                        this, this.onSelectProperty);
            }

            details.prototype.onAddProperty = function(event) {
                this.dialog = tibco.admin.Main.loadUIResource(tibco.admin.Main.getServer().getBodyBlock(), "tibco.admin",
                        "tibco/admin/enterpriseAssets/shdResDefs/JNDI/components/shdJndiResDefAddAttribute.xml", true);

                this.dialog.getDescendantOfName("btnOK_25D").subscribe(details.ONADDOK, this, this.onAddPropertyOK);
//                this.dialog.getDescendantOfName("txtNameAddProperty_25D").subscribe(details.ONCHANGEPROPERTY,
//                        this, this.onChangeProperty);
//                this.dialog.getDescendantOfName("txtValueAddProperty_25D").subscribe(details.ONCHANGEPROPERTY,
//                        this, this.onChangeProperty);
//                this.dialog.getDescendantOfName("txtValueAddProperty_25D").subscribe(details.ONCHANGEPROPERTYKEYDOWN, this, this.onChangePropertyKeyDown);
                this.dialog.getDescendantOfName("selTypeAddProperty_25D").subscribe(details.ONCHANGETYPE, this, this.onChangeType);
            }

            details.prototype.onChangeProperty = function(obj, strValue) {
                try {
                var objName = this.dialog.getDescendantOfName("txtNameAddProperty_25D");
                var objType = this.dialog.getDescendantOfName("selTypeAddProperty_25D");
                var objValue = this.dialog.getDescendantOfName("txtValueAddProperty_25D");
                var objNameStr = (((obj == objName) && strValue) ? strValue : objName.getValue());
                var objTypeStr = (((obj == objType) && strValue) ? strValue : objType.getValue());
                var objValueStr = (((obj == objValue) && strValue) ? strValue : objValue.getValue());
                if(!strValue) {
                    strValue = obj.getValue();
                }
                var valuePassed = false;
                switch(objTypeStr.toLowerCase()) {
                    case "byte":
                        if(objValueStr) {
                            valuePassed = tibco.admin.util.Validation.validateNumeric(objValue, objValueStr);
                            if(valuePassed) {
                                valuePassed = (objValueStr >= 0) && (objValueStr <= 255);
                                if(valuePassed) {
                                    obj.setBackgroundColor("white", true);
                                    objValue.setTip("");
                                }
                                else {
                                    obj.setBackgroundColor("yellow", true);
                                    objValue.setTip("'" + objValueStr + "' is not a number between 0 and 255");
                                }
                            }
                            else {
                                obj.setBackgroundColor("yellow", true);
                            }
                        }
                        else {
                            obj.setBackgroundColor("yellow", true);
                            objValue.setTip("Value is empty");
                        }
                        break;
                    case "double":
                        valuePassed = tibco.admin.util.Validation.validateNumeric(objValue, objValueStr);
                        break;
                    case "float":
                        valuePassed = tibco.admin.util.Validation.validateNumeric(objValue, objValueStr);
                        break;
                    case "int":
                        valuePassed = tibco.admin.util.Validation.doValidateNumbersOnly(objValue, objValueStr);
                        break;
                    case "long":
                        valuePassed = tibco.admin.util.Validation.doValidateNumbersOnly(objValue, objValueStr);
                        break;
                    case "short":
                        valuePassed = tibco.admin.util.Validation.doValidateNumbersOnly(objValue, objValueStr);
                        break;
                    case "string":
                        valuePassed = true;
                        break;
                    default:
                        valuePassed = tibco.admin.util.Validation.doValidateNotEmpty(objValue, objValueStr, tibco.admin.Main.getServer().getDynamicProperty("@@ValueAddProperty_25D"));
                        break;
                }

                var namePassed = tibco.admin.util.Validation.doValidateNotEmptyNoWhSpace(objName, objNameStr, tibco.admin.Main.getServer().getDynamicProperty("@@NameAddProperty_25D"));
                if(namePassed) {
                    var typePassed = tibco.admin.util.Validation.doValidateNotEmpty(objType, objTypeStr, tibco.admin.Main.getServer().getDynamicProperty("@@TypeAddProperty_25D"));
//                    if(typePassed) {
//                    }
                }
                var allPassed = true;
                if(namePassed && typePassed && valuePassed) {
                    // if type is "boolean", validates if the value is "true" or "false";
                    if(objTypeStr.toLowerCase() == "boolean") {
                        var lowerCaseValue = objValueStr.toLowerCase();
                        if((lowerCaseValue == "true") || (lowerCaseValue == "false")) {
                            tibco.admin.util.Validation.actionOnValidation(objValue, true);
                            allPassed = true;
                        }
                        else {
                            tibco.admin.util.Validation.actionOnValidation(objValue, false, tibco.admin.Main.getServer().getDynamicProperty("@@ValidationNotABooleanLabel", "'" + tibco.admin.Main.getServer().getDynamicProperty("@@ValueAddProperty_25D") + "'"));
                            allPassed = false;
                        }
                    }
                }
                else {
                    allPassed = false;
                }
                if(allPassed) {
                    this.dialog.getDescendantOfName("btnOK_25D").setEnabled(jsx3.gui.Form.STATEENABLED, true);
                }
                else {
                    this.dialog.getDescendantOfName("btnOK_25D").setEnabled(jsx3.gui.Form.STATEDISABLED, true);
                }
                } catch(ex) {
                    jsx3.log(ex);
                    tibco.admin.Main.logException(ex);
                }
            }

            details.prototype.onAddPropertyOK = function(event) {
                var objDoc = tibco.admin.Main.getServer().Cache.getDocument("xmlCacheIDAppPropertiesList_25D");
                var rec = objDoc.createNode(jsx3.xml.Entity.TYPEELEMENT, 'record');
                rec.setAttribute('jsxid', jsx3.xml.CDF.getKey());
                rec.setAttribute('name', this.dialog.getDescendantOfName("txtNameAddProperty_25D").getValue());
                rec.setAttribute('type', this.dialog.getDescendantOfName("selTypeAddProperty_25D").getValue());
                rec.setAttribute('value', this.dialog.getDescendantOfName("txtValueAddProperty_25D").getValue());
                objDoc.appendChild(rec);
                this.mainComponent.getDescendantOfName("mtxAppPropertiesList_25D").repaintData();
                this.dialog.doClose();
            }

            details.prototype.onSelectProperty = function(event) {
                this.mainComponent.getDescendantOfName("btnAppPropertiesRemove_25D").setEnabled(jsx3.gui.Form.STATEENABLED, true);
            }

            details.prototype.onRemoveProperty = function(event) {
                var matrix = this.mainComponent.getDescendantOfName("mtxAppPropertiesList_25D");
                var nodes = matrix.getSelectedNodes();
                for(var i = 0; i < nodes.size(); i++) {
                    matrix.deleteRecord(nodes.getItem(i).getAttribute('jsxid'));
                }
                matrix.repaintData();
            }

            details.prototype.ongetConfigDetailsSuccess = function(objEvent) {
                var response = tibco.admin.Main.getServer().Cache.getDocument("xmlCacheIDAppPropertiesList_25D").getRecordNode("jsxroot");
                this.jsxsuper(response);

                var value = response.getAttribute("connectionFactory");
                this.mainComponent.getDescendantOfName("lblcCtxtFactory_25D").setText(value, true);
                this.mainComponent.getDescendantOfName("cmbCtxtFactory_25D").setValue(value, true);
                value = response.getAttribute("contextURL");
                this.mainComponent.getDescendantOfName("lblcCtxtURL_25D").setText(value, true);
                this.mainComponent.getDescendantOfName("txtCtxtURL_25D").setValue(value, true);
                value = response.getAttribute("username");
                this.mainComponent.getDescendantOfName("lblcUserName_25D").setText(value ? value : "", true);
                this.mainComponent.getDescendantOfName("txtUserName_25D").setValue(value, true);
                value = response.getAttribute("password");
                // TODO: set password field length based on real password length?
                this.mainComponent.getDescendantOfName("lblcPassword_25D").setText("********", true);
                this.mainComponent.getDescendantOfName("txtPassword_25D").setValue(value, true);
                value = response.getAttribute("validateSecurityContext");
                this.mainComponent.getDescendantOfName("lblcValSecCtxt_25D").setText((value == "true") ? "Yes" : "No", true);
                this.mainComponent.getDescendantOfName("radValSecCtxtYes_25D").setGroupValue(value, true);

                this.mainComponent.getDescendantOfName("mtxAppPropertiesList_25D").repaintData();
                this.setEditable(true);
                this.initializeJNDIParam(objEvent);
            }

            details.prototype.setEditRequestDetails = function(objService) {
                this.setRequestDetails(objService);
            }

            details.prototype.setAddRequestDetails = function(objService) {
                this.setRequestDetails(objService);
            }

            details.prototype.setRequestDetails = function(objService) {
                objService.connectionFactory = this.mainComponent.getDescendantOfName("cmbCtxtFactory_25D").getValue();
                objService.contextURL = this.mainComponent.getDescendantOfName("txtCtxtURL_25D").getValue();
                objService.username = this.mainComponent.getDescendantOfName("txtUserName_25D").getValue();
                objService.password = this.mainComponent.getDescendantOfName("txtPassword_25D").getValue();
                objService.validateSecurityContext = this.mainComponent.getDescendantOfName("radValSecCtxtYes_25D").getGroupValue();
            }


            /*070818
               * if user doesn't fishish all the required fields, the "save" button will be disabled.  part 2 for jndi
               */
            details.prototype.JNDIFactory = function(obj) {
                this.passedItems["JNDIFactory"] = tibco.admin.util.Validation.doValidateNotEmptyNoWhSpace(obj, null, tibco.admin.Main.getServer().getDynamicProperty("@@CtxtFactory_25D"));
                this.enableSaveBtn4Add();
            }

            details.prototype.JNDIURL = function(obj, strValue) {
//                jsx3.log("Object: " + obj);
//                jsx3.log("Value: " + strValue);
                this.passedItems["JNDIURL"] = tibco.admin.util.Validation.doValidateNotEmptyNoWhSpace(obj, strValue, tibco.admin.Main.getServer().getDynamicProperty("@@CtxtURL_25D"));
                this.enableSaveBtn4Add();
            }

            details.prototype.initializeJNDIParam = function(objEvent) {
                this.passedItems["JNDIFactory"] = tibco.admin.util.Validation.trimAll(this.mainComponent.getDescendantOfName("cmbCtxtFactory_25D").getValue())?true:false;
                this.JNDIURL(this.mainComponent.getDescendantOfName("txtCtxtURL_25D"));
                this.enableSaveBtn4Add();
            }

            details.ONCHANGEPROPERTYKEYDOWN = "tibco.admin.enterpriseAssets.shdResDefs.JNDI.CHANGEPROPERTYKEYDOWN";
            details.prototype.onChangePropertyKeyDown = function(obj, strValue) {
            }

            details.ONCHANGETYPE = "tibco.admin.enterpriseAssets.shdResDefs.JNDI.CHANGETYPE";

            details.prototype.onChangeType = function(event) {
                this.dialog.getDescendantOfName("txtValueAddProperty_25D").setValue("");
            }

        });
